package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.UserDto;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;

import com.itheima.reggie.utils.ValidateCodeUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    //手机短信获取验证码
    @PostMapping("/sendMsg")
    public R<String> sendMessage(@RequestBody User user, HttpServletRequest request) {
        log.info("user:{}", user);
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成随机验证码
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            log.info("验证码是code:{}", code);
            //发送短信,没有资质验证
            // SMSUtils.sendMessage("瑞吉外卖","阿里云的秘钥Id",phoneNumber,code.toString());

            //将生成的验证码保存到session中,为了登录验证
            //request.getSession().setAttribute(phone, code);

            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            log.info("phone:{},code:{}", phone, code);
            String stringCode = String.valueOf(code);
            return R.success(stringCode);
        }
        return R.fail("短信发送失败");

    }

    //登录
    @PostMapping("/login")
    public R<String> login(@RequestBody UserDto userDto, HttpServletRequest request) {
        log.info("userDto:{}", userDto);
        //判断验证码是否正确
        String phone = userDto.getPhone();
        Integer code = userDto.getCode();
        //Integer getCode = (Integer) request.getSession().getAttribute(phone);
        Object getCode = redisTemplate.opsForValue().get(phone);
        boolean result = getCode != null ? getCode.equals(code) : false;
        if (!result) {
            return R.fail("验证码错误");
        }

        //判断是否是新用户,是新用户就保存到表中直接完成注册登录
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getPhone, phone);
        User userOne = userService.getOne(qw);
        log.info("userOne:{}", userOne);
        if (userOne == null) {
            User user = new User();
            BeanUtils.copyProperties(userDto, user);
            userService.save(user);
            log.info("新用户,用户唯一id:{}", user.getId());
            request.getSession().setAttribute("user", user.getId());
        } else {
            log.info("老用户,用户唯一id:{}", userOne.getId());
            request.getSession().setAttribute("user", userOne.getId());
        }
        redisTemplate.delete(phone);
        return R.success("登录成功");
    }

}
